package com.eccg.shopping.activity;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import com.eccg.shopping.R;
import com.eccg.shopping.utils.ListViewUtil;
import com.eccg.shopping.utils.PageView;
import com.eccg.shopping.utils.Store;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView.ScaleType;

public class StoreActivity extends BaseActivity {

	private Gallery singlestore_gallery;
	private PageView singlestore_pageview;
	private ListView singlestore_container_lv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.singlestore);
		
		Bundle bundle = getIntent().getExtras();
		Store store = bundle.getParcelable("singlestore");

		singlestore_gallery = (Gallery) findViewById(R.singlestore.singlestore_gallery);
		singlestore_pageview = (PageView) findViewById(R.singlestore.singlestore_pageview);
		singlestore_container_lv = (ListView) findViewById(R.singlestore.singlestore_container_lv);
		
		ArrayList<String> lists = getStrings();
		
		if(lists != null && lists.size() > 0){
			singlestore_pageview.setTotalPage(lists.size());
			singlestore_pageview.setCurrentPage(0);
		}
		
		PromotionAdapter promotionAdapter = new PromotionAdapter(StoreActivity.this, R.layout.promotion_item, R.promotion_item.promotion_item_tv, lists);
		singlestore_gallery.setAdapter(promotionAdapter);
		singlestore_gallery.setFadingEdgeLength(0);
		singlestore_gallery.setSpacing(5);
		singlestore_gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				singlestore_pageview.setCurrentPage(position);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});

		ContainerAdapter containerAdapter = new ContainerAdapter(StoreActivity.this, R.layout.singlestore_item, R.singlestore_item.singlestore_item_content_tv, lists);
		singlestore_container_lv.setAdapter(containerAdapter);
		singlestore_container_lv.setFadingEdgeLength(0);
		singlestore_container_lv.setDivider(null);
		ListViewUtil.setListViewHeightBasedOnChildren(singlestore_container_lv);
	}

	@Override
	protected int getMenuIndex() {
		return 0;
	}

	class ContainerAdapter extends ArrayAdapter<String> {
		public ContainerAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
			super(context, resource, textViewResourceId, objects);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.singlestore_item, null);
			}

			String content = getItem(position);
			TextView singlestore_item_top_tv = (TextView) convertView.findViewById(R.singlestore_item.singlestore_item_top_tv);

			if (position == 0) {
				singlestore_item_top_tv.setTextColor(getResources().getColor(R.color.top1));
				singlestore_item_top_tv.setText("TOP  1");
			} else if (position == 1) {
				singlestore_item_top_tv.setTextColor(getResources().getColor(R.color.top2));
				singlestore_item_top_tv.setText("TOP  2");
			} else if (position == 2) {
				singlestore_item_top_tv.setTextColor(getResources().getColor(R.color.top3));
				singlestore_item_top_tv.setText("TOP  3");
			}else if(position > 8){
				singlestore_item_top_tv.setTextColor(getResources().getColor(R.color.top4));
				singlestore_item_top_tv.setText("TOP" + (position + 1));
			} 
			
			else {
				singlestore_item_top_tv.setTextColor(getResources().getColor(R.color.top4));
				singlestore_item_top_tv.setText("TOP  " + (position + 1));
			}

			TextView singlestore_item_content_tv = (TextView) convertView.findViewById(R.singlestore_item.singlestore_item_content_tv);
			singlestore_item_content_tv.setText(content);

			return convertView;
		}
	}

	class PromotionAdapter extends ArrayAdapter<String> {

		public PromotionAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
			super(context, resource, textViewResourceId, objects);
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.promotion_item, null);
			}
			ImageView imageView = (ImageView) convertView.findViewById(R.promotion_item.promotion_item_iv);
			imageView.setImageResource(R.drawable.image_size);
			imageView.setScaleType(ScaleType.FIT_XY);
			return convertView;
		}
	}
	
	private ArrayList<String> getStrings() {
		ArrayList<String> lists = new ArrayList<String>();
		lists.add("夏季长衫你会穿吗");
		lists.add("夏季长衫你会穿吗");
		lists.add("夏季长衫你会穿吗");
		lists.add("夏季长衫你会穿吗");
		lists.add("夏季长衫你会穿吗");
		lists.add("夏季长衫你会穿吗");
		lists.add("夏季长衫你会穿吗");
		lists.add("夏季长衫你会穿吗");
		lists.add("夏季长衫你会穿吗");
		lists.add("夏季长衫你会穿吗");
		return lists;
	}

	@Override
	protected void initHeader() {
		// TODO Auto-generated method stub
		super.initHeader();
		Bundle bundle = getIntent().getExtras();
		Store store = bundle.getParcelable("singlestore");
		titleTx.setText(store.getStoreName());
		Button backBtn = new Button(this);
		backBtn.setText("返回");
		backBtn.setTextSize(16);
		backBtn.setTextColor(getResources().getColor(R.color.white));
		backBtn.setGravity(Gravity.CENTER);
		backBtn.setBackgroundResource(R.drawable.left_btn_bg);
		backBtn.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		leftTitle.removeAllViews();
		leftTitle.addView(backBtn);
		
		Button daohangBtn = new Button(this);
		daohangBtn.setText("导航");
		daohangBtn.setTextSize(16);
		daohangBtn.setTextColor(getResources().getColor(R.color.white));
		daohangBtn.setGravity(Gravity.CENTER);
		daohangBtn.setBackgroundResource(R.drawable.right_btn_bg);
		daohangBtn.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(StoreActivity.this, MendianDaohangActivity.class);
				StoreActivity.this.startActivity(intent);
			}
		});
		rightTitle.removeAllViews();
		rightTitle.addView(daohangBtn);
	}
}
